#!/usr/bin/env python

import sys, getopt, scipy, tables, datetime
from avy_app import Avy, UT_Avy_Adviso, CO_Avy_Adviso, date_str

import os
os.chdir(r'C:\Users\Paul\workspace\avy-app\server_scripts')

def create_if_needed(node, subnode, nodetype):
    pass

def new_database(rose=False, snow=False):
    '''create a new h5 file for the avalance application
    '''
    avy = Avy()

    class Rose(tables.IsDescription):
        ''' data format for the danger rose'''
        date = tables.StringCol(10)
        colors = tables.StringCol(24)
        statement = tables.StringCol(1000)
        created_by = tables.StringCol(50)


    class Snow(tables.IsDescription):
        ''' data format for the snow conditions'''
        hour = tables.StringCol(13)
        depth = tables.FloatCol()
        temp = tables.FloatCol()
        swe = tables.FloatCol()

    # open the file
    h5file = tables.openFile('avy-app.h5', mode='w', title="Avalanche Application Data")
    #h5file.flush()
    # make container for zones information
    if 'zones' not in dir(h5file.root) or type(f.root.zones) is not tables.Group:
        zonesgroup = h5file.createGroup(h5file.root, 'zones', "Avalanche Assessment Zones")
    # make each zone
    for zone in avy.zones:
        zone_name = zone.short_name
        if zone not in dir(h5file.root.zones):
            zonegroup = h5file.createGroup(
                zonesgroup, zone.short_name, 'Zone Data for '+zone.zone_name)
        # make a table for the danger roses
        #if 'rose' not in dir(zonegroup) or dir(zonegroup.rose)!=dir(Rose):
        if rose:
            rosetable = h5file.createTable(zonegroup, 'rose', Rose)
        if snow:
            snowtable = h5file.createTable(zonegroup, 'snow', Rose)
    # clean up
    h5file.flush()
    h5file.close()





def append_daily_roses():
    h5file = tables.openFile('avy-app.h5', mode='a')
    avy = Avy()
    
    for zone in avy.zones:
        today = date_str(datetime.datetime.today())
        # just check the last one?
        tbl = h5file.getNode('/zones/'+zone.short_name).rose
        filed_dates = [r['date'] for r in tbl.iterrows()]
        #print filed_dates, today
        
        
        if today not in filed_dates or True:

            if zone.state.upper() == 'UT':
                adviso = UT_Avy_Adviso(zone.short_name)
            elif zone.state.upper() == 'CO':
                adviso = CO_Avy_Adviso(zone.short_name)
            
            dstr = date_str(adviso.date)
            print zone.short_name, dstr, today
            if dstr == today:
                tbl.row['date'] = dstr
                tbl.row['colors'] = adviso.rose_colors
                tbl.row['statement'] = adviso.statement
                tbl.row['created_by'] = adviso.created_by
                print adviso.rose_colors
            else:
                tbl.row['date'] = today
                tbl.row['colors'] = '0'*24
                tbl.row['statement'] = 'NA'
                tbl.row['created_by'] = 'NA'
            tbl.row.append()
        h5file.flush()
    h5file.close()


def test2():# Read in and display the data sets.
    filename = Filename
    h5file = tables.openFile(filename, 'r')
    dataset1Obj = h5file.getNode('/Datasets/Phase1', 'dataset1')
    dataset2Obj = h5file.getNode('/Datasets/Phase1', 'dataset2')
    print repr(dataset1Obj)
    print repr(dataset2Obj)
    dataset1Array = dataset1Obj.read()
    dataset2Array = dataset2Obj.read()
    print 'type(dataset1Array):', type(dataset1Array)
    print 'type(dataset2Array):', type(dataset2Array)
    print 'array1:\n', dataset1Array
    print 'array2:\n', dataset2Array
    # print several slices of our array.
    print 'slice [0]:', dataset1Array[0]
    print 'slice [0:2]:',dataset1Array[0:2]
    print 'slice [1, 0:4:2]:',dataset1Array[1, 0:4:2]
    h5file.close()



main = append_daily_roses
main()
